<template>
	<view class="login-container">
		<view class="header">
			<image class="logo" src="/static/img/logo.png" mode="aspectFit"></image>
			<text class="title">PetCloud</text>
			<text class="subtitle">宠物云平台</text>
		</view>
		
		<view class="content">
			<view class="button-area">
				<!-- 微信登录按钮 -->
				<button 
					class="wx-button" 
					type="primary" 
					:loading="isLoading"
					:disabled="isLoading"
					@click="handleLogin">
					<u-icon name="weixin-fill" color="#ffffff" size="36"></u-icon>
					<text>{{ isLoading ? '登录中...' : '微信一键登录' }}</text>
				</button>
			</view>
			
			<view class="tips">
				<text>登录代表您已同意</text>
				<navigator url="/pages/agreement/privacy" class="link">《隐私政策》</navigator>
				<text>和</text>
				<navigator url="/pages/agreement/service" class="link">《用户协议》</navigator>
			</view>
		</view>
	</view>
</template>

<script>
// 导入微信登录示例模块
import WechatLogin from './wechatLogin';

export default {
	data() {
		return {
			isLoading: false,
			errorMsg: ''
		}
	},
	onLoad() {
		// 检查是否已登录
		this.checkLoginStatus();
	},
	methods: {
		// 检查登录状态
		async checkLoginStatus() {
			try {
				const user = await WechatLogin.checkLoginStatus();
				if (user) {
					// 已登录，直接跳转到主页
					this.navigateToIndex();
				}
			} catch (error) {
				console.error('检查登录状态失败:', error);
			}
		},
		
		// 登录按钮点击事件
		handleLogin() {
			if (this.isLoading) return;
			
			this.isLoading = true;
			this.errorMsg = '';
			
			// 调用微信登录
			WechatLogin.login()
				.then(result => {
					console.log('登录成功:', result);
					// 登录成功，跳转到首页
					this.navigateToIndex();
				})
				.catch(error => {
					console.error('登录失败:', error);
					uni.showToast({
						title: '登录失败，请重试',
						icon: 'none'
					});
				})
				.finally(() => {
					this.isLoading = false;
				});
		},
		
		// 跳转到首页
		navigateToIndex() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		}
	}
}
</script>

<style lang="scss">
.login-container {
	height: 100vh;
	display: flex;
	flex-direction: column;
	background-color: #f8f8f8;
	
	.header {
		flex: 2;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 80rpx;
		
		.logo {
			width: 180rpx;
			height: 180rpx;
			margin-bottom: 30rpx;
		}
		
		.title {
			font-size: 48rpx;
			font-weight: bold;
			color: #333;
			margin-bottom: 15rpx;
		}
		
		.subtitle {
			font-size: 32rpx;
			color: #666;
		}
	}
	
	.content {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: flex-end;
		padding: 0 60rpx 80rpx 60rpx;
		
		.button-area {
			margin-bottom: 50rpx;
			
			.wx-button {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				background-color: #07c160;
				height: 90rpx;
				border-radius: 45rpx;
				box-shadow: 0 10rpx 20rpx rgba(7, 193, 96, 0.2);
				
				text {
					margin-left: 15rpx;
					font-size: 32rpx;
				}
				
				&::after {
					border: none;
				}
			}
		}
		
		.tips {
			text-align: center;
			font-size: 24rpx;
			color: #999;
			display: flex;
			justify-content: center;
			align-items: center;
			
			.link {
				color: #5199fe;
				display: inline-block;
			}
		}
	}
}
</style> 